#!/usr/bin/env python
import os, os.path as path, futils
Import('build')

Env = build.env.Clone()
Env.Append(CPPDEFINES=[('KROLL_API_EXPORT', 1)])
build.add_thirdparty(Env, 'poco')

webkit_tp = path.join(build.third_party, 'webkit')
webkit_lib = path.join(webkit_tp, 'lib')
webkit_inc = path.join(webkit_tp, 'include')

if build.is_win32():
	Env.Append(CPPPATH=[path.join(build.third_party, 'icu', 'include')])
	Env.Append(LIBPATH=[webkit_lib])
	Env.Append(LIBS=['shell32', 'user32', 'Advapi32', 'iphlpapi', 'winhttp', 'icuuc', 'icuin'])
	Env.Append(CCFLAGS=['/DUNICODE'])
	Env.Append(CCFLAGS=['/D_UNICODE'])

elif build.is_linux():
	if os.environ.has_key('PKG_CONFIG_PATH'):
		Env.AppendENVPath('PKG_CONFIG_PATH', os.environ['PKG_CONFIG_PATH'])
	Env.ParseConfig('pkg-config --cflags --libs libxml-2.0')
	Env.Append(LIBS=['pthread', 'libsoup-2.4', 'libproxy'])

	# We need to add the webkit directories because they include libsoup
	Env.Append(LIBPATH=[webkit_lib])
	Env.Append(CPPPATH=[webkit_inc])

elif build.is_osx():
	Env.Append(LINKFLAGS='-install_name libkroll.dylib')
	Env.Append(FRAMEWORKS=['SystemConfiguration', 'CoreServices'])

sources=Glob('*.cpp')
sources.extend(Glob('config/*.cpp'))
sources.extend(Glob('binding/*.cpp'))
sources.extend(Glob('utils/*.cpp'))
sources.extend(Glob('utils/poco/*.cpp'))
sources.extend(Glob('utils/%s/*.cpp' % build.os))
sources.extend(Glob('net/proxy_config.cpp'))
sources.extend(Glob('net/*_%s.cpp' % build.os))
if build.is_win32():
	sources.extend(Glob('utils/unzip/*.cpp'))
	sources.extend(Glob('win32/*.cpp'))
if build.is_osx():
	sources.extend(Glob('osx/osx.mm'))
	sources.extend(Glob('utils/%s/*.mm' % build.os))
if build.is_osx() or build.is_linux():
	sources.extend(Glob('utils/posix/*.cpp'))
	sources.extend(Glob('net/*_%s.mm' % build.os))

# copy the includes for building the SDK
kroll_include = path.join(build.kroll_include_dir, 'kroll')
futils.CopyTree(build.cwd(), kroll_include, include=['.h'])

t = Env.SharedLibrary(build.dir +'/runtime/kroll', source=sources)
build.mark_build_target(t)
